/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package engine;

import DataModel.EntryStatus;
import DataModel.NotaryEntry;
import TransportLayer.IServiceListener;
import SynchronizationLayer.ISynchronizationServer;
import TransportLayer.NotaryRequestPacket;
import TransportLayer.NotaryResponse;
import TransportLayer.NotaryResponseType;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import Repository.IRepositoryProxy;

/**
 *
 * @author Tomasz Nowak
 */
class RevocationHandler implements IRequestHandler
{
	IRepositoryProxy repositoryProxy;
	IServiceListener serviceListener;
	ISynchronizationServer synchronizationServer;

	public RevocationHandler( IRepositoryProxy repository, IServiceListener serviceListener, ISynchronizationServer synchronizationServer )
	{
		this.repositoryProxy = repository;
		this.serviceListener = serviceListener;
		this.synchronizationServer = synchronizationServer;
	}

	public IRepositoryProxy getRepositoryProxy()
	{
		return repositoryProxy;
	}

	public void setRepositoryProxy( IRepositoryProxy repositoryProxy )
	{
		this.repositoryProxy = repositoryProxy;
	}

	public IServiceListener getServiceListener()
	{
		return serviceListener;
	}

	public void setServiceListener( IServiceListener serviceListener )
	{
		this.serviceListener = serviceListener;
	}

	public ISynchronizationServer getSynchronizationClient()
	{
		return synchronizationServer;
	}

	public void setSynchronizationClient( ISynchronizationServer synchronizationServer )
	{
		this.synchronizationServer = synchronizationServer;
	}

	@Override
	public void handleRequest( NotaryRequestPacket request )
	{
		NotaryResponse response = null;
		try
		{
			NotaryEntry entry = repositoryProxy.getEntry(request.getNotaryRequest().getNotaryCertificate().getName());
			if ( !request.getNotaryRequest().getNotaryCertificate().equals(entry.getNotaryCertificate()) )
			{
				response = new NotaryResponse(NotaryResponseType.ERROR_CertificateNotFound, null);
			}
			else
			{
				if ( verifyCertificateOwner(request) )
				{
					this.repositoryProxy.updateEntryStatus(entry, EntryStatus.Revocated);

					this.synchronizationServer.revoke(entry);

					response = new NotaryResponse(NotaryResponseType.OK_Revocation,
																				request.getNotaryRequest().getNotaryCertificate());
				}
				else
				{
					response = new NotaryResponse(NotaryResponseType.ERROR_CertificateValidationFailure, null);
				}
			}
			serviceListener.sendResponse(response, request.getSourceAddress(), request.getSourcePort());
		}
		catch ( IOException ex )
		{
			Logger.getLogger(RevocationHandler.class.getName()).log(Level.SEVERE, null, ex);
		}
		catch ( ClassNotFoundException ex )
		{
			Logger.getLogger(RevocationHandler.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	//todo:write
	private boolean verifyCertificateOwner( NotaryRequestPacket request )
	{
		return serviceListener.verifyClient(request);
	}
}
